// 先引入包
const express = require('express')
const path = require('path')
const cors = require('cors')
const {server} = require('./config')
const routers = require('./router')

// 创建 express 对象
const app = express()

const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))

app.use('/uploads', express.static('uploads'))

// 解决 AJAX 跨域
app.use(cors())


/************** 注册路由 -- 开始 **************/
routers.forEach(v=>{
    let router = require(path.join(__dirname,'routers',v.file))
    if(router instanceof Array) {
        router.forEach(v1=>{
            app.use(v.path, v1)
        })
    } else {
        app.use(v.path, router)
    }
})


// 启动项目
app.listen(server.port, server.ip, ()=>{
    console.log(`启动成功！监听：${server.ip}:${server.port}`);
})